package cn.tedu.ivos.application.controller;

import cn.tedu.ivos.application.pojo.dto.ApplicationQueryDTO;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParamDTO;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.base.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/application")
public class ApplicationController {

    @Autowired
    ApplicationService applicationService;

    /**
     * 保存一个application申请对象
     * @param saveParams
     * @return
     */
    @PostMapping(value={"/saveApp", "/save"})
    public JsonResult saveApplication(ApplicationSaveParamDTO saveParams){
        log.debug("ApplicationController-saveApplication{}",saveParams);
        applicationService.saveApplication(saveParams);
        //注意： 保存一个application申请对象的时候，同时还要保存一条audit审批对象
        //(所以service层要执行AuditMapper和ApplicationMapper中的两个插入方法)
        return JsonResult.ok();
    }

    /**查询所有申请*/
    @GetMapping(value={"/getAppList","/select"})
    public JsonResult getAppList(ApplicationQueryDTO appQueryDTO){
        log.debug("ApplicationController-getAppList{}",appQueryDTO);
        List<ApplicationVO> appVOList=applicationService.selectApplications(appQueryDTO);
        return JsonResult.ok(appVOList);
    }

    /**取消申请*/
    @PostMapping(value={"/cancelApp/{id}","/cancel/{id}"})
    public JsonResult cancelApp(@PathVariable("id") Long id){
        log.debug("ApplicationController-cancelApp{}",id);
        applicationService.cancelApp(id);
        return JsonResult.ok();
    }

    @Operation(summary = "分配车辆")
    @ApiOperationSupport(order = 40)
    @PostMapping("distribute/{applicationId}/{vehicleId}")
    public JsonResult distribute(@PathVariable("applicationId") Long applicationId, @PathVariable("vehicleId") Long vehicleId){
        log.debug("分配车辆参数:申请单编号:{},车辆编号:{}",applicationId,vehicleId);
        applicationService.distribute(applicationId,vehicleId);
        return JsonResult.ok();
    }


    @Operation(summary = "还车")
    @ApiOperationSupport(order = 50)
    @PostMapping("back/{applicationId}/{vehicleId}")
    public JsonResult back(@PathVariable("applicationId") Long applicationId, @PathVariable("vehicleId") Long vehicleId){
        log.debug("还车参数:申请单编号:{},车辆编号:{}",applicationId,vehicleId);
        applicationService.back(applicationId,vehicleId);
        return JsonResult.ok();
    }


}